package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Daily;
import cn.wolfcode.trip.base.domain.DailyContent;
import cn.wolfcode.trip.base.query.DailyQuery;
import cn.wolfcode.trip.base.service.IDailyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("daily")
public class DailyController {
    @Autowired
    private IDailyService dailyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") DailyQuery qo, Model model) {
        //根据创建时间倒序
        qo.setOrderBy("createTime desc");
        model.addAttribute("pageInfo", dailyService.query(qo));
        return "daily/list";
    }
   @RequestMapping("getContentById")
    @ResponseBody
    public DailyContent getContentById(Long id) {
        return dailyService.getContentById(id);
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(MultipartFile file, Daily entity) {
        if (file != null && file.getSize() > 0) {
            String upload = UploadUtil.uploadQiniu(file);
            entity.setCoverUrl(UploadUtil.QINIU_PATH + "/" + upload);
        }
        dailyService.saveOrUpdate(entity);
        return "redirect:/daily/list.do";
    }
    @RequestMapping("delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        dailyService.delete(id);
        return new JSONResult();
    }
}
